package cn1shared.net;

import java.io.InputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;


public class JsonClient extends JsonCon {

	public JsonClient(DataInputStream in,DataOutputStream out) {
		super(in,out);
	}

	public Object call(String cmd,Object args,InputStream... ins) throws JsonCmdException, IOException {
		List<Object> request = Arrays.asList(cmd,args);
		send(request,ins);
		List<Object> response = receive();
		switch( response.size() ) {
		case 1:
			return response.get(0);
		case 3:
			String code = (String)response.get(0);
			String msg = (String)response.get(1);
			Object extra = response.get(2);
			throw new JsonCmdException(code,msg,extra);
		default:
			throw new RuntimeException(response.toString());
		}
	}
}
